﻿namespace HutongGames.PlayMaker
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using UnityEngine;

    public static class FsmUtility
    {
        public static Fsm CurrentFsm;
        private static UTF8Encoding encoding;

        public static Color ByteArrayToColor(byte[] bytes, int startIndex)
        {
            float r = BitConverter.ToSingle(bytes, startIndex);
            float g = BitConverter.ToSingle(bytes, startIndex + 4);
            float b = BitConverter.ToSingle(bytes, startIndex + 8);
            return new Color(r, g, b, BitConverter.ToSingle(bytes, startIndex + 12));
        }

        public static FsmBool ByteArrayToFsmBool(byte[] bytes, int startIndex, int totalLength)
        {
            string varName = Encoding.GetString(bytes, startIndex + 2, totalLength - 2);
            if (varName != string.Empty)
            {
                return CurrentFsm.GetFsmBool(varName);
            }
            FsmBool bool2 = new FsmBool();
            bool2.Value = BitConverter.ToBoolean(bytes, startIndex);
            bool2.UseVariable = BitConverter.ToBoolean(bytes, startIndex + 1);
            return bool2;
        }

        public static FsmColor ByteArrayToFsmColor(byte[] bytes, int startIndex, int totalLength)
        {
            string varName = Encoding.GetString(bytes, startIndex + 0x11, totalLength - 0x11);
            if (varName != string.Empty)
            {
                return CurrentFsm.GetFsmColor(varName);
            }
            FsmColor color2 = new FsmColor();
            color2.Value = ByteArrayToColor(bytes, startIndex);
            color2.UseVariable = BitConverter.ToBoolean(bytes, startIndex + 0x10);
            return color2;
        }

        public static FsmEvent ByteArrayToFsmEvent(byte[] bytes, int startIndex, int size)
        {
            string str = ByteArrayToString(bytes, startIndex, size);
            if (!string.IsNullOrEmpty(str))
            {
                return FsmEvent.GetFsmEvent(str);
            }
            return null;
        }

        public static FsmFloat ByteArrayToFsmFloat(byte[] bytes, int startIndex, int totalLength)
        {
            string varName = Encoding.GetString(bytes, startIndex + 5, totalLength - 5);
            if (varName != string.Empty)
            {
                return CurrentFsm.GetFsmFloat(varName);
            }
            FsmFloat num2 = new FsmFloat();
            num2.Value = BitConverter.ToSingle(bytes, startIndex);
            num2.UseVariable = BitConverter.ToBoolean(bytes, startIndex + 4);
            return num2;
        }

        public static FsmInt ByteArrayToFsmInt(byte[] bytes, int startIndex, int totalLength)
        {
            string varName = Encoding.GetString(bytes, startIndex + 5, totalLength - 5);
            if (varName != string.Empty)
            {
                return CurrentFsm.GetFsmInt(varName);
            }
            FsmInt num2 = new FsmInt();
            num2.Value = BitConverter.ToInt32(bytes, startIndex);
            num2.UseVariable = BitConverter.ToBoolean(bytes, startIndex + 4);
            return num2;
        }

        public static FsmQuaternion ByteArrayToFsmQuaternion(byte[] bytes, int startIndex, int totalLength)
        {
            string varName = Encoding.GetString(bytes, startIndex + 0x11, totalLength - 0x11);
            if (varName != string.Empty)
            {
                return CurrentFsm.GetFsmQuaternion(varName);
            }
            FsmQuaternion quaternion2 = new FsmQuaternion();
            quaternion2.Value = ByteArrayToQuaternion(bytes, startIndex);
            quaternion2.UseVariable = BitConverter.ToBoolean(bytes, startIndex + 0x10);
            return quaternion2;
        }

        public static FsmRect ByteArrayToFsmRect(byte[] bytes, int startIndex, int totalLength)
        {
            string varName = Encoding.GetString(bytes, startIndex + 0x11, totalLength - 0x11);
            if (varName != string.Empty)
            {
                return CurrentFsm.GetFsmRect(varName);
            }
            FsmRect rect2 = new FsmRect();
            rect2.Value = ByteArrayToRect(bytes, startIndex);
            rect2.UseVariable = BitConverter.ToBoolean(bytes, startIndex + 0x10);
            return rect2;
        }

        public static FsmVector3 ByteArrayToFsmVector3(byte[] bytes, int startIndex, int totalLength)
        {
            string varName = Encoding.GetString(bytes, startIndex + 13, totalLength - 13);
            if (varName != string.Empty)
            {
                return CurrentFsm.GetFsmVector3(varName);
            }
            FsmVector3 vector2 = new FsmVector3();
            vector2.Value = ByteArrayToVector3(bytes, startIndex);
            vector2.UseVariable = BitConverter.ToBoolean(bytes, startIndex + 12);
            return vector2;
        }

        public static Quaternion ByteArrayToQuaternion(byte[] bytes, int startIndex)
        {
            float x = BitConverter.ToSingle(bytes, startIndex);
            float y = BitConverter.ToSingle(bytes, startIndex + 4);
            float z = BitConverter.ToSingle(bytes, startIndex + 8);
            return new Quaternion(x, y, z, BitConverter.ToSingle(bytes, startIndex + 12));
        }

        public static Rect ByteArrayToRect(byte[] bytes, int startIndex)
        {
            float left = BitConverter.ToSingle(bytes, startIndex);
            float top = BitConverter.ToSingle(bytes, startIndex + 4);
            float width = BitConverter.ToSingle(bytes, startIndex + 8);
            return new Rect(left, top, width, BitConverter.ToSingle(bytes, startIndex + 12));
        }

        public static string ByteArrayToString(byte[] bytes)
        {
            if (bytes.Length == 0)
            {
                return "";
            }
            return Encoding.GetString(bytes);
        }

        public static string ByteArrayToString(byte[] bytes, int startIndex, int count)
        {
            if (count == 0)
            {
                return string.Empty;
            }
            return Encoding.GetString(bytes, startIndex, count);
        }

        public static Vector2 ByteArrayToVector2(byte[] bytes, int startIndex)
        {
            float x = BitConverter.ToSingle(bytes, startIndex);
            return new Vector2(x, BitConverter.ToSingle(bytes, startIndex + 4));
        }

        public static Vector3 ByteArrayToVector3(byte[] bytes, int startIndex)
        {
            float x = BitConverter.ToSingle(bytes, startIndex);
            float y = BitConverter.ToSingle(bytes, startIndex + 4);
            return new Vector3(x, y, BitConverter.ToSingle(bytes, startIndex + 8));
        }

        public static Vector4 ByteArrayToVector4(byte[] bytes, int startIndex)
        {
            float x = BitConverter.ToSingle(bytes, startIndex);
            float y = BitConverter.ToSingle(bytes, startIndex + 4);
            float z = BitConverter.ToSingle(bytes, startIndex + 8);
            return new Vector4(x, y, z, BitConverter.ToSingle(bytes, startIndex + 12));
        }

        public static ICollection<byte> ColorToByteArray(Color color)
        {
            List<byte> list = new List<byte>();
            list.AddRange(BitConverter.GetBytes(color.r));
            list.AddRange(BitConverter.GetBytes(color.g));
            list.AddRange(BitConverter.GetBytes(color.b));
            list.AddRange(BitConverter.GetBytes(color.a));
            return list;
        }

        public static ICollection<byte> FsmBoolToByteArray(FsmBool fsmBool)
        {
            if (fsmBool == null)
            {
                fsmBool = new FsmBool();
            }
            List<byte> list = new List<byte>();
            list.AddRange(BitConverter.GetBytes(fsmBool.Value));
            list.AddRange(BitConverter.GetBytes(fsmBool.UseVariable));
            list.AddRange(StringToByteArray(fsmBool.Name));
            return list;
        }

        public static ICollection<byte> FsmColorToByteArray(FsmColor fsmColor)
        {
            if (fsmColor == null)
            {
                fsmColor = new FsmColor();
            }
            List<byte> list = new List<byte>();
            list.AddRange(ColorToByteArray(fsmColor.Value));
            list.AddRange(BitConverter.GetBytes(fsmColor.UseVariable));
            list.AddRange(StringToByteArray(fsmColor.Name));
            return list;
        }

        public static ICollection<byte> FsmEventToByteArray(FsmEvent fsmEvent)
        {
            if (fsmEvent == null)
            {
                return null;
            }
            List<byte> list = new List<byte>();
            list.AddRange(StringToByteArray(fsmEvent.Name));
            return list;
        }

        public static ICollection<byte> FsmFloatToByteArray(FsmFloat fsmFloat)
        {
            if (fsmFloat == null)
            {
                fsmFloat = new FsmFloat();
            }
            List<byte> list = new List<byte>();
            list.AddRange(BitConverter.GetBytes(fsmFloat.Value));
            list.AddRange(BitConverter.GetBytes(fsmFloat.UseVariable));
            list.AddRange(StringToByteArray(fsmFloat.Name));
            return list;
        }

        public static ICollection<byte> FsmIntToByteArray(FsmInt fsmInt)
        {
            if (fsmInt == null)
            {
                fsmInt = new FsmInt();
            }
            List<byte> list = new List<byte>();
            list.AddRange(BitConverter.GetBytes(fsmInt.Value));
            list.AddRange(BitConverter.GetBytes(fsmInt.UseVariable));
            list.AddRange(StringToByteArray(fsmInt.Name));
            return list;
        }

        public static ICollection<byte> FsmQuaternionToByteArray(FsmQuaternion fsmQuaternion)
        {
            if (fsmQuaternion == null)
            {
                fsmQuaternion = new FsmQuaternion();
            }
            List<byte> list = new List<byte>();
            list.AddRange(QuaternionToByteArray(fsmQuaternion.Value));
            list.AddRange(BitConverter.GetBytes(fsmQuaternion.UseVariable));
            list.AddRange(StringToByteArray(fsmQuaternion.Name));
            return list;
        }

        public static ICollection<byte> FsmRectToByteArray(FsmRect fsmRect)
        {
            if (fsmRect == null)
            {
                fsmRect = new FsmRect();
            }
            List<byte> list = new List<byte>();
            list.AddRange(RectToByteArray(fsmRect.Value));
            list.AddRange(BitConverter.GetBytes(fsmRect.UseVariable));
            list.AddRange(StringToByteArray(fsmRect.Name));
            return list;
        }

        public static ICollection<byte> FsmVector3ToByteArray(FsmVector3 fsmVector3)
        {
            if (fsmVector3 == null)
            {
                fsmVector3 = new FsmVector3();
            }
            List<byte> list = new List<byte>();
            list.AddRange(Vector3ToByteArray(fsmVector3.Value));
            list.AddRange(BitConverter.GetBytes(fsmVector3.UseVariable));
            list.AddRange(StringToByteArray(fsmVector3.Name));
            return list;
        }

        public static string GetPath(FsmState state)
        {
            if (state == null)
            {
                return "[missing state]";
            }
            return (((state.Fsm != null) ? (state.Fsm.OwnerDebugName + ": " + state.Fsm.Name) : "[missing FSM]") + ": " + state.Name + ": ");
        }

        public static System.Type GetType(string typeName)
        {
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (System.Type type in assembly.GetExportedTypes())
                {
                    if (typeName == type.ToString())
                    {
                        return type;
                    }
                }
            }
            return null;
        }

        public static Texture2D LoadDllResource(string resourceName, int width, int height)
        {
            if (Application.isWebPlayer)
            {
                return null;
            }
            Texture2D textured = (Texture2D) Resources.Load(resourceName);
            if (textured == null)
            {
                textured = new Texture2D(width, height, TextureFormat.ARGB32, false);
                Stream manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("HutongGames.PlayMaker.Resources." + resourceName + ".png");
                if (manifestResourceStream != null)
                {
                    textured.LoadImage(ReadToEnd(manifestResourceStream));
                    manifestResourceStream.Close();
                    return textured;
                }
                Debug.LogError("Missing Dll resource: " + resourceName);
            }
            return textured;
        }

        public static ICollection<byte> QuaternionToByteArray(Quaternion quaternion)
        {
            List<byte> list = new List<byte>();
            list.AddRange(BitConverter.GetBytes(quaternion.x));
            list.AddRange(BitConverter.GetBytes(quaternion.y));
            list.AddRange(BitConverter.GetBytes(quaternion.z));
            list.AddRange(BitConverter.GetBytes(quaternion.w));
            return list;
        }

        private static byte[] ReadToEnd(Stream stream)
        {
            byte[] buffer4;
            long position = stream.Position;
            stream.Position = 0L;
            try
            {
                int num3;
                byte[] buffer = new byte[0x1000];
                int offset = 0;
                while ((num3 = stream.Read(buffer, offset, buffer.Length - offset)) > 0)
                {
                    offset += num3;
                    if (offset == buffer.Length)
                    {
                        int num4 = stream.ReadByte();
                        if (num4 != -1)
                        {
                            byte[] buffer2 = new byte[buffer.Length * 2];
                            Buffer.BlockCopy(buffer, 0, buffer2, 0, buffer.Length);
                            Buffer.SetByte(buffer2, offset, (byte) num4);
                            buffer = buffer2;
                            offset++;
                        }
                    }
                }
                byte[] dst = buffer;
                if (buffer.Length != offset)
                {
                    dst = new byte[offset];
                    Buffer.BlockCopy(buffer, 0, dst, 0, offset);
                }
                buffer4 = dst;
            }
            finally
            {
                stream.Position = position;
            }
            return buffer4;
        }

        public static ICollection<byte> RectToByteArray(Rect rect)
        {
            List<byte> list = new List<byte>();
            list.AddRange(BitConverter.GetBytes(rect.x));
            list.AddRange(BitConverter.GetBytes(rect.y));
            list.AddRange(BitConverter.GetBytes(rect.width));
            list.AddRange(BitConverter.GetBytes(rect.height));
            return list;
        }

        public static byte[] StringToByteArray(string str)
        {
            if (str == null)
            {
                str = "";
            }
            return Encoding.GetBytes(str);
        }

        public static string StripNamespace(string name)
        {
            if (name == null)
            {
                return "[missing name]";
            }
            string[] strArray = name.Split(new char[] { Convert.ToChar(".") });
            if (strArray.Length <= 0)
            {
                return name;
            }
            return strArray[strArray.Length - 1];
        }

        public static ICollection<byte> Vector2ToByteArray(Vector2 vector2)
        {
            List<byte> list = new List<byte>();
            list.AddRange(BitConverter.GetBytes(vector2[0]));
            list.AddRange(BitConverter.GetBytes(vector2[1]));
            return list;
        }

        public static ICollection<byte> Vector3ToByteArray(Vector3 vector3)
        {
            List<byte> list = new List<byte>();
            list.AddRange(BitConverter.GetBytes(vector3[0]));
            list.AddRange(BitConverter.GetBytes(vector3[1]));
            list.AddRange(BitConverter.GetBytes(vector3[2]));
            return list;
        }

        public static ICollection<byte> Vector4ToByteArray(Vector4 vector4)
        {
            List<byte> list = new List<byte>();
            list.AddRange(BitConverter.GetBytes(vector4[0]));
            list.AddRange(BitConverter.GetBytes(vector4[1]));
            list.AddRange(BitConverter.GetBytes(vector4[2]));
            list.AddRange(BitConverter.GetBytes(vector4[3]));
            return list;
        }

        public static UTF8Encoding Encoding
        {
            get
            {
                return (encoding ?? (encoding = new UTF8Encoding()));
            }
        }

        public static class BitConverter
        {
            public static byte[] GetBytes(bool value)
            {
                if (System.BitConverter.IsLittleEndian)
                {
                    return System.BitConverter.GetBytes(value);
                }
                byte[] bytes = System.BitConverter.GetBytes(value);
                Array.Reverse(bytes);
                return bytes;
            }

            public static byte[] GetBytes(int value)
            {
                if (System.BitConverter.IsLittleEndian)
                {
                    return System.BitConverter.GetBytes(value);
                }
                byte[] bytes = System.BitConverter.GetBytes(value);
                Array.Reverse(bytes);
                return bytes;
            }

            public static byte[] GetBytes(float value)
            {
                if (System.BitConverter.IsLittleEndian)
                {
                    return System.BitConverter.GetBytes(value);
                }
                byte[] bytes = System.BitConverter.GetBytes(value);
                Array.Reverse(bytes);
                return bytes;
            }

            public static bool ToBoolean(byte[] value, int startIndex)
            {
                return System.BitConverter.ToBoolean(value, startIndex);
            }

            public static int ToInt32(byte[] value, int startIndex)
            {
                if (!System.BitConverter.IsLittleEndian)
                {
                    Array.Reverse(value, startIndex, 4);
                }
                return System.BitConverter.ToInt32(value, startIndex);
            }

            public static float ToSingle(byte[] value, int startIndex)
            {
                if (!System.BitConverter.IsLittleEndian)
                {
                    Array.Reverse(value, startIndex, 4);
                }
                return System.BitConverter.ToSingle(value, startIndex);
            }
        }
    }
}

